package tiepbm.app.store.activity;

import tiepbm.app.store.Common;
import tiepbm.app.util.Util;
import tiepbm.app.util.hardware.KeyboardUtil;
import tiepbm.app.util.text.ValidateUtil;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class RegisterActivity extends BaseActivity implements IScreen{

	private EditText mUsernameEdt;
	private EditText mPassEdt;
	private EditText mRetypePassEdt;
	private EditText mEmailEdt;
	
	private Button mRegisterBt;
	
	private View mErrorView;

	private String mUserName;
	private String mPass;
	private String mEmail;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		initBase();
		initView();
		implementListener();
	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		mUsernameEdt = (EditText) findViewById(R.id.TxtLoginUsername);
		mPassEdt = (EditText) findViewById(R.id.TxtPassword);
		mRetypePassEdt = (EditText) findViewById(R.id.TxtPasswordRepeat);
		mEmailEdt = (EditText) findViewById(R.id.TxtLoginEmail);
		mRegisterBt = (Button) findViewById(R.id.btnSignin);
	}

	@Override
	public void implementListener() {
		// TODO Auto-generated method stub
		mRegisterBt.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub


						boolean cancel = validateField();
						if (cancel) {
							mErrorView.requestFocus();
							return;
						} else {
							KeyboardUtil.hideSoftKeyboard(RegisterActivity.this);
						}

						// Check network
						if (!Util.isNetworking(RegisterActivity.this)) {
							Util.showDialog(Common.CODE_ERROR_CONNECT, RegisterActivity.this);
							return;
						}
			}
		});
	}

	@Override
	public void fillData() {
		// TODO Auto-generated method stub
		
	}
	/**
	 * Check valid all fields in edit text
	 * 
	 * @return
	 */
	private boolean validateField() {

		/* Reset errors */
		mUsernameEdt.setError(null);
		mPassEdt.setError(null);
		mRetypePassEdt.setError(null);
		mEmailEdt.setError(null);

		mUserName = mUsernameEdt.getText().toString();
		mPass = mPassEdt.getText().toString();
		String retypePass = mRetypePassEdt.getText().toString();
		mEmail = mEmailEdt.getText().toString();

		boolean cancel = false;

		/* Check valid username */
		if (TextUtils.isEmpty(mUserName)) {
			mUsernameEdt
					.setError(getString(R.string.error_username_null));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateUserName(mUserName)) {
			mUsernameEdt
					.setError(getString(R.string.error_username));
			mErrorView = mUsernameEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid password */
		if (TextUtils.isEmpty(mPass) || mPass.length() < 6) {
			mPassEdt.setError(getString(R.string.error_password_length));
			mErrorView = mPassEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid retype password */
		if (!retypePass.equals(mPass)) {
			mRetypePassEdt
					.setError(getString(R.string.error_password_retype));
			mErrorView = mRetypePassEdt;
			cancel = true;
			return cancel;
		}

		/* Check valid email */
		if (TextUtils.isEmpty(mEmail)) {
			mEmailEdt.setError(getString(R.string.error_email_null));
			mErrorView = mEmailEdt;
			cancel = true;
			return cancel;
		} else if (!ValidateUtil.validateEmail(mEmail)) {
			mEmailEdt.setError(getString(R.string.error_email));
			mErrorView = mEmailEdt;
			cancel = true;
			return cancel;
		}
		return cancel;
	}
}
